﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Framework.Utils
{

    public static class DictionaryUtils
    {
        public static V Get<K,V>(IDictionary<K,V> obj, K key, V defaultValue)
        {
            return (obj.ContainsKey(key)) ? obj[key] : defaultValue;
        }
    }

    public static class ListUtils
    {



        #region filter

        //过滤委托
        public delegate bool DFilter<T>(T data, int index);

        private delegate V Getter<T, V>(T data, int index);

        //查找出符合条件的对象
        private static List<V> FindAll<T, V>(List<T> list, DFilter<T> filter, Getter<T, V> getter, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return null; }   //空

            //过滤遍历函数
            List<V> findList = new List<V>();
            int findCount = 0;
            for (int i = 0; i < count; i++)
            {
                T data = list[i];
                if (data == null) { continue; }
                // check
                if (!filter(data, i)) { continue; }
                //保存数组
                findList.Add(getter(data, i));
                //记录增加
                findCount++;
                if (maxCount > 0 && findCount >= maxCount) { break; } //数量足够, 跳出处理

            }
            return findList;
        }

        //查找出符合条件的对象
        private static List<T> FindAll<T>(List<T> list, DFilter<T> filter, int maxCount)
        {
            return FindAll(list, filter, delegate (T data, int index) { return data; }, maxCount);
        }

        //查找出符合条件的一个对象
        public static T FindOne<T>(List<T> list, DFilter<T> filter)
        {
            //过滤遍历函数
            List<T> findList = FindAll(list, filter, 1);
            //检测查找对象
            int count = (findList != null) ? findList.Count : 0;
            if (count <= 0)
            {
                return default(T);  //没找到对象
            }
            //返回第一个
            return findList[0];
        }


        #endregion


        #region remove

        //移除符合条件的数据
        public static List<T> RemoveAll<T>(List<T> list, DFilter<T> filter, int maxCount)
        {
            return RemoveAll(list, filter, (T data, int index) => { return data; }, maxCount);
        }

        //移除符合条件的数据
        private static List<V> RemoveAll<T, V>(List<T> list, DFilter<T> filter, Getter<T, V> getter, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return null; }   //空

            //遍历执行
            int removeCount = 0;
            var retList = new List<V>();
            for (int i = 0; i < count; i++)
            {
                T data = list[i];
                if (data == null) { continue; }
                //执行处理
                if (!filter(data, i)) { continue; }

                // add
                var get = getter(data, i);
                if (get != null) { retList.Add(get); }

                // remove
                removeCount++;
                count--;
                i--;
                if (maxCount > 0 && removeCount >= maxCount) { return null; }
            }
            return retList;
        }





        #endregion


        #region action

        //遍历委托
        public delegate bool DAction<T>(T data, int index);

        //遍历执行
        public static int Action<T>(List<T> list, DAction<T> action, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return 0; }   //空

            //遍历执行
            for (int i = 0; i < count; i++)
            {
                T data = list[i];
                if (data == null) { continue; }
                //执行处理
                if (!action(data, i)) { return i; }
                if (maxCount > 0 && i >= maxCount) { return i; }
            }
            return count;
        }

        // 倒序执行
        public static int ActionD<T>(List<T> list, DAction<T> action, int maxCount)
        {
            int count = (list != null) ? list.Count : 0;
            if (count <= 0) { return 0; }   //空

            //遍历执行
            int actionCount = 0;
            for (int i = count + 1; i >= 0; i--)
            {
                T data = list[i];
                if (data == null) { continue; }

                actionCount++;
                //执行处理
                if (!action(data, i)) { return actionCount; }
                if (maxCount > 0 && actionCount >= maxCount) { return i; }
            }
            return actionCount;
        }




        #endregion




        //public static int Compare<T>(T a, T b)
        //{
        //    return (a > b) ? 1 : ((a == b) ? 0 : -1);
        //}
    }

}